package com.rustic.server.controller.admapi;

import cn.hutool.core.collection.CollUtil;
import cn.xy.base.server.utils.PageUtil;
import com.rustic.server.enums.CategoryType;
import com.rustic.server.query.CategoryAdminListQuery;
import com.rustic.server.dto.CategoryAddDTO;
import com.rustic.server.dto.CategoryUpdateDTO;
import com.rustic.server.po.Category;
import com.rustic.server.service.CategoryService;
import com.rustic.server.vo.list.CategoryAdminListVO;
import com.rustic.server.vo.CategoryVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.annotation.OperateLog;
import cn.xy.commons.core.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 表[category]对应的后台管理类
 *
 * @author auto
 *
 */

@Slf4j
@RestController
@RequestMapping("/admapi/category")
@Api(tags = {"分类管理接口"})
public class CategoryAdminController {

    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "分类-列表", notes = "权限key: category-list")
    @GetMapping("/list")
    @PreAuthorize("@validate.hasPermission('category-list')")
    public Result<Page<CategoryAdminListVO>> list(@Validated CategoryAdminListQuery query) {
        LambdaQueryWrapper<Category> queryWrapper = categoryService.toAdminListWrapper(query);
        if (queryWrapper == null) {
            return Result.data(new Page<>());
        }
        Page<Category> pageData = categoryService.page(query.toPage(), queryWrapper);
        List<Category> categoryList = pageData.getRecords();
        if (CollUtil.isEmpty(categoryList)) {
            return Result.data(new Page<>());
        }
        List<CategoryAdminListVO> results = categoryService.toAdminListVo(categoryList);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "分类-无分页列表")
    @GetMapping("/listJsonByType")
    public Result<List<Category>> listJsonByType(int type) {
        if (type == 0) {
            return Result.data(categoryService.list());
        }
        List<Category> categoryList = categoryService.listByType(CategoryType.toCategoryType(type));
        return Result.data(categoryList);
    }

    @ApiOperation(value = "分类-详情", notes = "权限key: category-detail")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "分类id"),
    })
    @GetMapping("/detail")
    @PreAuthorize("@validate.hasPermission('category-detail')")
    public Result<CategoryVO> detail(int id) {
        if (id <= 0) {
            return Result.error(CategoryService.CATEGORY_NOT_EXIST);
        }
        Category category = categoryService.getById(id);
        if (category == null) {
            return Result.error(CategoryService.CATEGORY_NOT_EXIST);
        }
        CategoryVO categoryVO = new CategoryVO(category);
        return Result.data(categoryVO);
    }

    @ApiOperation(value = "分类-增加", notes = "权限key: category-add")
    @PostMapping("/add")
    @PreAuthorize("@validate.hasPermission('category-add')")
    @OperateLog(description = "增加分类")
    public Result<String> add(@RequestBody @Validated CategoryAddDTO categoryAddDTO) {
        Category category = categoryAddDTO.toCategory();
        categoryService.save(category);
        return Result.ok("添加分类成功");
    }

    @ApiOperation(value = "分类-更新", notes = "权限key: category-update")
    @PutMapping("/update")
    @PreAuthorize("@validate.hasPermission('category-update')")
    @OperateLog(description = "更新分类")
    public Result<String> update(@RequestBody @Validated CategoryUpdateDTO categoryUpdateDTO) {
        Category category = categoryService.getById(categoryUpdateDTO.getId());
        if (category == null) {
            return Result.error(CategoryService.CATEGORY_NOT_EXIST);
        }
        BeanUtils.copyProperties(categoryUpdateDTO, category);
        categoryService.updateById(category);
        return Result.ok("编辑分类成功");
    }

    @ApiOperation(value = "分类-删除", notes = "权限key: category-delete")
    @DeleteMapping("/delete")
    @PreAuthorize("@validate.hasPermission('category-delete')")
    @OperateLog(description = "删除分类")
    public Result<String> delete(int id) {
        if (id <= 0) {
            return Result.error(CategoryService.CATEGORY_NOT_EXIST);
        }
        Category category = categoryService.getById(id);
        if (category == null) {
            return Result.error(CategoryService.CATEGORY_NOT_EXIST);
        }
        categoryService.removeById(id);
        return Result.ok("删除分类成功");
    }
}
